<template>
	<view><baseForm :column.sync="column"  @submitFun="submitFun" @changeColumn="changeColumn" :submitStatus="submitDisable"></baseForm></view>
</template>

<script>
import baseForm from '@/wxcomponents/form/form.vue';
import pickerModule from '@/wxcomponents/form/picker.vue';
import { format, dectList } from '@/libs/common.js';
import common from '@/libs/login.js';
import userMixins from '@/libs/userMixins.js'
import formMixins from '@/libs/formMixins.js';
export default {
	mixins: [userMixins, formMixins],
	data() {
		let self = this;
		return {
			leaveReason: '',
			column: [
				{
					label: '请假类型',
					name: 'leaveType',
					type: 'picker',
					required: true,
					disabled: false,
					value: '',
					text: '请选择请假类型',
					mode: 'selector', //picker的类型
					range: self.findOneDictList('dict_leave_type')
				},
				{
					label: '请假原因', 
					name: 'leaveReason',
					type: 'picker',
					required: true,
					disabled: false,
					value: '',
					text: '请选择请假原因',
					mode: 'selector', //picker的类型
					range: self.findOneDictList('dict_leave_reason')
				},
				{
					label: '请假详情',
					name: 'reasonDetail',
					type: 'normalInput',
					value: '',
					text: '请输入请假原因',
					hidden: true
				},
				{
					label: '起始日期',
					value: '',
					required: true,
					name: 'startDate',
					type: 'picker',
					mode: 'date',
					text: '请选择',
					start:format('YY-mm-dd',new Date())
				},
				
				{
					label: '起始时间',
					value: '',
					required: true,
					name: 'startTime',
					type: 'picker',
					mode: 'time',
					text: '请选择',
				},
				{
					label: '结束日期',
					value: '',
					required: true,
					name: 'endDate',
					type: 'picker',
					mode: 'date',
					text: '请选择',
					start:format('YY-mm-dd',new Date())
				},
				{
					label: '结束时间',
					value: '',
					required: true,
					name: 'endTime',
					type: 'picker',
					mode: 'time',
					text: '请选择',
				},
				{
					label: '附件(最多上传三张)',
					type: 'upload',
					url: '/stu/common/uploadFile',
					attachmentVoListStr: '',
					limitSize:3,
					disabled:false
				}
			]
		};
	},
	onShow() {
		// 监听字段变化
		this.EventBus.on('columnItemChange',(obj) => {
			if (obj.key == 'leaveReason' && obj.value) {
				this.leaveReason = obj.value;
			}
		})
	},
	onLoad() {
		
	},
	methods: {
		//提交或修改请假申请
		async submitFun(id) {
			let params = {
				leaveType: this.column[0].value,
				leaveReason: this.leaveReason,
				reasonDetail: this.column[2].value,
				startTime: `${this.column[3].text} ${this.column[4].text}`,
				endTime: `${this.column[5].text} ${this.column[6].text}`,
				attachmentVoListStr: this.column[7].attachmentVoListStr,
			}
			let rules = [
				{
					key: 'leaveType',
					checkFunc(value) {
						if (!value)	return false;
						return true;
					},
					errMsg: '请选择请假类型'
				},
				{
					key: 'leaveReason',
					checkFunc(value) {
						if (!value)	return false;
						return true;
					},
					errMsg: '请选择请假原因'
				}
			]
			let checkRes = this.checkFormIsPassRules(params, rules);
			if (!checkRes.isPass) {
				uni.showToast({
					title: checkRes.errMsg,
					icon: 'none',
					duration: 2000,
				});
				return;
			}
			let resp = await this.$http.post({
				url: '/oa/leaveApply/save',
				data: params
			})
			this.showToast('提交成功',()=> {uni.navigateBack()});
		},
		/**
		 * 清空原有请假原因，计算当前请假原因范围	1开头：因公	2开头：因私
		 * @param {Int} leaveType	当前请假类型 
		 * */
		filterLeaveReasonChoice(leaveType) {
			if (!leaveType) return;
			this.column[1].value = '';
			let choices = this.findOneDictList('dict_leave_reason');
			let range = choices.filter(item => {
				let str = item.value + '';
				return str.substring(0, 1) == leaveType
			});
			this.column[1].range = range;
		},
		/**
		 * 计算详细原因字段展示状态
		 * 请假原因为"其他"时展示
		 * @param {Int} leaveReasonName 	请假原因
		 * */
		calDetailReasonShowStatus(leaveReasonName) {
			this.column[2].hidden = leaveReasonName != '其他';
		},
		// hanldeChangeColumn(column) {
		// 	// 特殊处理不响应的列
		// 	this.leaveReason = Object.assign({}, column[1], {});
		// 	debugger;
		// }
	},
	components: {
		baseForm,
		pickerModule
	},
	computed: {
		leaveType() {
			return this.column[0].value
		},
		leaveReasonName() {
			let column = this.column[1];
			return column.text
		}
	},
	watch: {
		leaveType: {
			handler: function(leaveType) {
				this.filterLeaveReasonChoice(leaveType)
			},
			deep: true
		},
		leaveReasonName: {
			handler: function(leaveReasonName) {
				this.calDetailReasonShowStatus(leaveReasonName)
			},
			deep: true
		}
	}
};
</script>

<style></style>
